package com.example.dryyzx.controller;

import com.example.dryyzx.dao.NursingContentMapper;
import com.example.dryyzx.dao.NursingLevelMapper;
import com.example.dryyzx.encapsulation.*;
import com.example.dryyzx.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class NursingLevelController {

    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Autowired
    private NursingContentMapper nursingContentMapper;
    public List<NursingPageEncapsulation> search_nursinglevel(int limit,int offset,String level){
            List<NursingLevel> nursingLevels = nursingLevelMapper.selectLevelByLimitAndOffset(limit,offset,level);
            List<NursingPageEncapsulation> targetLevels = new ArrayList<NursingPageEncapsulation>();
            if (nursingLevels.size()!= 0) {
                for (int i = 0; i < nursingLevels.size(); i++) {
                    NursingPageEncapsulation tmp = new NursingPageEncapsulation();
                    tmp.id = nursingLevels.get(i).getId();
                    tmp.level = level;
                    if (nursingLevels.get(i).getEnable() == true) {
                        tmp.status = "启用";
                    } else tmp.status = "禁用";
                    tmp.remarks = nursingLevels.get(i).getDescription();
                    targetLevels.add(i, tmp);
                }
            }
            return targetLevels;
    }
   public NursingPageEncapsulation3 getContentByLevel(int id,String fre,int time){
        NursingPageEncapsulation3 content =new NursingPageEncapsulation3();
        content.id=id;
        content.fre=fre;
        content.time=time;
        content.name=nursingContentMapper.selectByPrimaryKey(id).getName();
        content.remarks=nursingContentMapper.selectByPrimaryKey(id).getRemarks();
        return content;
    }
    @RequestMapping(value = "nursingchoose_page/search_by_level", method = RequestMethod.POST)
    public @ResponseBody
    String search_nursinglevel_by_level(int page,String level, HttpSession session) {
        if(level.equals("一级") | level.equals("二级") | level.equals("三级") | level.equals("四级"))
        {
            int data_per_page =6;
            int total_data_no =nursingLevelMapper.countByLevel(level);
            if(total_data_no<=0) return new JsonResult(false,"找不到该级别的信息！").toString();

            int offset=(page-1)*data_per_page;
            int limit;
            if(total_data_no-offset<data_per_page)
                limit=total_data_no-offset;
            else
                limit=data_per_page;
            if(total_data_no<=offset)
                return new JsonResult(false, "页数过大，数据不够").toString();

            List<NursingPageEncapsulation> target=search_nursinglevel(limit,offset,level);

            NursingPageEncapsulation2 total_data=new NursingPageEncapsulation2();
            total_data.total_page=(total_data_no-1)/data_per_page+1;
            total_data.nursing_level_list=target;
            return new JsonResult(true, total_data).toString();
        }else  return new JsonResult(false, "请输入正确的等级（1~4）！").toString();
    }
    @RequestMapping(value = "nursingchoose_page/getNursing", method = RequestMethod.POST)
    public @ResponseBody
    String getNursing(int page, HttpSession session) {
        int data_per_page = 6;
        int total_data_no = nursingLevelMapper.countLevels();
        if(total_data_no<=0)
            return new JsonResult(false, "护理等级表为空").toString();

        int offset=(page-1)*data_per_page;
        int limit;
        if(total_data_no-offset<data_per_page)
            limit=total_data_no-offset;
        else
            limit=data_per_page;
        if(total_data_no<=offset)
            return new JsonResult(false, "页数过大，数据不够").toString();

        List<NursingLevel> levels=nursingLevelMapper.selectByLimitAndOffset(limit,offset);
        List<NursingPageEncapsulation> targetLevels = new ArrayList<NursingPageEncapsulation>();
        for(int i=0;i<limit;i++){
            NursingPageEncapsulation tmp = new NursingPageEncapsulation();
            tmp.id = levels.get(i).getId();
            tmp.level = levels.get(i).getLevel();
            if (levels.get(i).getEnable() == true) {
                tmp.status = "启用";
            } else tmp.status = "禁用";
            tmp.remarks = levels.get(i).getDescription();
            targetLevels.add(i, tmp);
        }
        NursingPageEncapsulation2 total_data=new NursingPageEncapsulation2();
        total_data.nursing_level_list=targetLevels;
        total_data.total_page=(total_data_no-1)/data_per_page+1;
        return new JsonResult(true, total_data).toString();
    }
    @RequestMapping(value = "nursingchoose_page/addNursing", method = RequestMethod.POST)
    public @ResponseBody
    void addNursing(@RequestParam("level")String level,@RequestParam("status")String status,@RequestParam("remarks")String remarks, HttpSession session) {
        NursingLevel nursingLevel=new NursingLevel();
        //nursingLevel.setId(Integer.valueOf(id));
        nursingLevel.setLevel(level);
        if(status.equals("启用"))
            nursingLevel.setEnable(true);
        else nursingLevel.setEnable(false);
        nursingLevel.setDescription(remarks);
        nursingLevelMapper.insertSelective(nursingLevel);
    }
    @RequestMapping(value = "nursingchoose_page/delNursing", method = RequestMethod.POST)
    public @ResponseBody
    void delNursing(@RequestParam("id")String id,HttpSession session) {
        NursingLevel nursingLevel=nursingLevelMapper.selectByPrimaryKey(Integer.valueOf(id));
        nursingLevel.setEnable(false);
        nursingLevelMapper.updateByPrimaryKeySelective(nursingLevel);
    }
    @RequestMapping(value = "nursingchoose_page/editNursing", method = RequestMethod.POST)
    public @ResponseBody
    void editNursing(@RequestParam("id")String id,@RequestParam("level")String level,@RequestParam("status")String status,@RequestParam("remarks")String remarks, HttpSession session) {
        NursingLevel nursingLevel=nursingLevelMapper.selectByPrimaryKey(Integer.valueOf(id));
        nursingLevel.setLevel(level);
        if(status.equals("启用"))
            nursingLevel.setEnable(true);
        else nursingLevel.setEnable(false);
        nursingLevel.setDescription(remarks);
        nursingLevelMapper.updateByPrimaryKeySelective(nursingLevel);
    }
    @RequestMapping(value = "nursingconfig_page/getNursingConfig", method = RequestMethod.POST)
    public @ResponseBody
    String  getNursingConfig(String id) {
        NursingLevel targetLevel=nursingLevelMapper.selectByPrimaryKey(Integer.valueOf(id));
        List<NursingPageEncapsulation3> allContent=new ArrayList<NursingPageEncapsulation3>();
        if(targetLevel!=null){
            NursingPageEncapsulation3 tmp=new NursingPageEncapsulation3();
            if(targetLevel.getContentId1()!=null) {
                tmp=getContentByLevel(targetLevel.getContentId1(), targetLevel.getFre1(),targetLevel.getTime1());
                tmp.number="1";
                allContent.add(tmp);
            }
            if(targetLevel.getContentId2()!=null) {
                tmp=getContentByLevel(targetLevel.getContentId2(), targetLevel.getFre2(),targetLevel.getTime2());
                tmp.number="2";
                allContent.add(tmp);
            }
            if(targetLevel.getContentId3()!=null) {
                tmp=getContentByLevel(targetLevel.getContentId3(), targetLevel.getFre3(),targetLevel.getTime3());
                tmp.number="3";
                allContent.add(tmp);
            }
            if(targetLevel.getContentId4()!=null) {
                tmp=getContentByLevel(targetLevel.getContentId4(), targetLevel.getFre4(),targetLevel.getTime4());
                tmp.number="4";
                allContent.add(tmp);
            }
            if(targetLevel.getContentId5()!=null) {
                tmp=getContentByLevel(targetLevel.getContentId5(), targetLevel.getFre5(),targetLevel.getTime5());
                tmp.number="5";
                allContent.add(tmp);
            }
            if(targetLevel.getContentId6()!=null) {
                tmp=getContentByLevel(targetLevel.getContentId6(), targetLevel.getFre6(),targetLevel.getTime6());
                tmp.number="6";
                allContent.add(tmp);
            }
            return new JsonResult(true,allContent).toString();
        }else return new JsonResult(false,"找不到该护理级别的信息！").toString();
    }
    @RequestMapping(value = "nursingconfig_page/addNursingConfig", method = RequestMethod.POST)
    public @ResponseBody
    String  addNursingConfig(String id,String name,String fre,String time,String number,String remarks) {
        NursingContent addContent=nursingContentMapper.selectByName(name);
        NursingLevel targetLevel=nursingLevelMapper.selectByPrimaryKey(Integer.valueOf(id));
        boolean available=false;
        if(targetLevel!=null){
            if(number.equals("1")&&targetLevel.getContentId1()==null)
            {
                available=true;
                targetLevel.setContentId1(addContent.getId());
                targetLevel.setFre1(fre);
                targetLevel.setTime1(Integer.valueOf(time));
            }
            if(number.equals("2")&&targetLevel.getContentId2()==null)
            {
                available=true;
                targetLevel.setContentId2(addContent.getId());
                targetLevel.setFre2(fre);
                targetLevel.setTime2(Integer.valueOf(time));
            }
            if(number.equals("3")&&targetLevel.getContentId3()==null)
            {
                available=true;
                targetLevel.setContentId3(addContent.getId());
                targetLevel.setFre3(fre);
                targetLevel.setTime3(Integer.valueOf(time));
            }
            if(number.equals("4")&&targetLevel.getContentId4()==null)
            {
                available=true;
                targetLevel.setContentId4(addContent.getId());
                targetLevel.setFre4(fre);
                targetLevel.setTime4(Integer.valueOf(time));
            }
            if(number.equals("5")&&targetLevel.getContentId5()==null)
            {
                available=true;
                targetLevel.setContentId5(addContent.getId());
                targetLevel.setFre5(fre);
                targetLevel.setTime5(Integer.valueOf(time));
            }
            if(number.equals("6")&&targetLevel.getContentId6()==null)
            {
                available=true;
                targetLevel.setContentId6(addContent.getId());
                targetLevel.setFre6(fre);
                targetLevel.setTime6(Integer.valueOf(time));
            }
            if(available==true){
                addContent.setRemarks(remarks);
                nursingLevelMapper.updateByPrimaryKeySelective(targetLevel);
                nursingContentMapper.updateByPrimaryKeySelective(addContent);
                return new JsonResult(true,available).toString();//传过去的data 表示该content_id可填且已填
            }else return new JsonResult(false,"排序号已被占用！请查看已配置好的护理内容 再填写正常的排序号(范围需为1-6)").toString();
        }else return new JsonResult(false,"找不到该护理级别！").toString();
    }
    @RequestMapping(value = "nursingconfig_page/delNursingConfig", method = RequestMethod.POST)
    public @ResponseBody
    String  delNursingConfig(String id,String number) {
        NursingContent delContent=new NursingContent();
        NursingLevel targetLevel=nursingLevelMapper.selectByPrimaryKey(Integer.valueOf(id));
        boolean del=false;
        if(targetLevel!=null){
            if(number.equals("1")&&targetLevel.getContentId1()!=null)
            {
                del=true;
                nursingLevelMapper.setNull1(targetLevel);
            }
            if(number.equals("2")&&targetLevel.getContentId2()!=null)
            {
                del=true;
                nursingLevelMapper.setNull2(targetLevel);
            }
            if(number.equals("3")&&targetLevel.getContentId3()!=null)
            {
                del=true;
                nursingLevelMapper.setNull3(targetLevel);
            }
            if(number.equals("4")&&targetLevel.getContentId4()!=null)
            {
                del=true;
                nursingLevelMapper.setNull4(targetLevel);
            }
            if(number.equals("5")&&targetLevel.getContentId5()!=null)
            {
                del=true;
                nursingLevelMapper.setNull5(targetLevel);
            }

            if(number.equals("6")&&targetLevel.getContentId6()!=null)
            {
                del=true;
                nursingLevelMapper.setNull6(targetLevel);
            }
            if(del==true){
                return new JsonResult(true,del).toString();//传过去的data 表示该content_id已删
            }else return new JsonResult(false,"请输入正常的可删除排序号(范围需为1-6)").toString();
        }else return new JsonResult(false,"找不到该护理级别！").toString();
    }
    @RequestMapping(value = "nursingconfig_page/editNursingConfig", method = RequestMethod.POST)
    public @ResponseBody
    String  editNursingConfig(String id,String number,String name,String fre,String time,String remarks) {
        NursingContent editContent=nursingContentMapper.selectByName(name);
        NursingLevel targetLevel=nursingLevelMapper.selectByPrimaryKey(Integer.valueOf(id));
        boolean edit=false;
        if(targetLevel!=null){
            if(number.equals("1")&&targetLevel.getContentId1()!=null)
            {
                edit=true;
                targetLevel.setContentId1(editContent.getId());
                targetLevel.setFre1(fre);
                targetLevel.setTime1(Integer.valueOf(time));
            }
            if(number.equals("2")&&targetLevel.getContentId2()!=null)
            {
                edit=true;
                targetLevel.setContentId2(editContent.getId());
                targetLevel.setFre2(fre);
                targetLevel.setTime2(Integer.valueOf(time));
            }
            if(number.equals("3")&&targetLevel.getContentId3()!=null)
            {
                edit=true;
                targetLevel.setContentId3(editContent.getId());
                targetLevel.setFre3(fre);
                targetLevel.setTime3(Integer.valueOf(time));
            }
            if(number.equals("4")&&targetLevel.getContentId4()!=null)
            {
                edit=true;
                targetLevel.setContentId4(editContent.getId());
                targetLevel.setFre4(fre);
                targetLevel.setTime4(Integer.valueOf(time));
            }
            if(number.equals("5")&&targetLevel.getContentId5()!=null)
            {
                edit=true;
                targetLevel.setContentId5(editContent.getId());
                targetLevel.setFre5(fre);
                targetLevel.setTime5(Integer.valueOf(time));
            }

            if(number.equals("6")&&targetLevel.getContentId6()!=null)
            {
                edit=true;
                targetLevel.setContentId6(editContent.getId());
                targetLevel.setFre6(fre);
                targetLevel.setTime6(Integer.valueOf(time));
            }
            if(edit==true){
                editContent.setRemarks(remarks);
                nursingLevelMapper.updateByPrimaryKeySelective(targetLevel);
                nursingContentMapper.updateByPrimaryKeySelective(editContent);
                return new JsonResult(true,edit).toString();//传过去的data 表示该content_id已删
            }else return new JsonResult(false,"请查看已配置好的护理内容 再输入正常的排序号(范围需为1-6) 若排序号未被占用 请使用添加配置功能").toString();
        }else return new JsonResult(false,"找不到该护理级别！").toString();
    }

}
